package com.zjw.service

import scala.beans.BeanProperty

import com.zjw.dao.UserMapper
import com.zjw.model.{User, UserExample}
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class UserService {

  @Autowired
  @BeanProperty var userMapper: UserMapper = _

  def getUserById(userId: Int) = {
    userMapper.selectByPrimaryKey(userId)
  }

  def getUserByUsername(username: String): User = {
    val userExample = new UserExample
    userExample.createCriteria().andUsernameEqualTo(username)
    val result = userMapper.selectByExample(userExample)
    if (result.size() == 1) {
      result.get(0)
    } else {
      null
    }
  }

  def getUserByUsernameAndPassword(username: String, password: String): User = {
    val userExample = new UserExample
    userExample.createCriteria()
      .andUsernameEqualTo(username)
      .andPasswordEqualTo(password)
    val result = userMapper.selectByExample(userExample)
    if (result.size() == 1) {
      result.get(0)
    } else {
      null
    }
  }
}
